<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Nav;
use App\Http\Requests\Nav\Store;
use App\Http\Requests\Nav\Update;

class NavController extends Controller
{
    //
    public function index()
    {
        $nav   = Nav::withTrashed()->orderBy('sort')->get();
        $assign = compact('nav');

        return view('admin.nav.index', $assign);
    }
    public function create()
    {
        //
        return view('admin.nav.create');
    }
    public function store(Store $request)
    {
   //dd($request->except('_token'));exit;
        Nav::create($request->except('_token','q'));

        return redirect(url('admin/nav'));
    }
    public function show(Category $category)
    {
       $data   = Category::withTrashed()->orderBy('sort')->get();
        $assign = compact('data');
       dd('ddd');exit;
        return view('admin.category.index', $assign);
    }
    public function edit(Nav $nav)
    {
  
         $assign = compact('nav');
        return view('admin.nav.edit', $assign);
    }
    public function update(Update $request, Nav $nav)
    {
    	
      $nav->update($request->except('_token','q'));

        return redirect()->back();
    }
    public function destroy($id)
    {
        Nav::destroy($id);

        return redirect(url('admin/nav'));
      
    }
    //恢复删除
    public function restore($id)
    {
        Nav::onlyTrashed()->find($id)->restore();

        return redirect(url('admin/nav'));
    }
//彻底删除
    public function forceDelete($id)
    {
        Nav::onlyTrashed()->find($id)->forceDelete();

        return redirect(url('admin/nav'));
    }
    public function sort(Request $request, Nav $navModel)
    {
        $data     = $request->except('_token');
        $sortData = [];
        foreach ($data as $k => $v) {
            $sortData[] = [
                'id'   => $k,
                'sort' => $v,
            ];
        }
        $navModel->updateBatch('navs',$sortData);

        return redirect()->back();
    }

}
